﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;
using AliyunOss;
using Microsoft.AspNetCore.Http;

namespace AdminUI.Pages.BaseData
{
    public partial class TenantAddModel : BaseModel
    {
        readonly AliyunOssClient oss;
        public TenantAddModel(AliyunOssClient ossClient)
        {
            oss = ossClient;
        }

        public void OnGet()
        {
            //var nodeId = Request.Query["nodeId"];
            //var nodeText = Request.Query["nodeText"];
            //ViewBag.AreaSelectedText = $"{nodeId}-{nodeText}";
            //ViewBag.AreaSelectedValue = nodeId;

            //ViewBag.WxAppList = WxAppBO.Instance.GetList(true).Where(p => p.WxAppType == 1); 
        }

        public IActionResult OnPostBtnSubmit_Click(TenantAddDto dto, IFormFile logoUrl, IFormFile imageUrl)
        {
            try
            {
                //if (logoUrl != null)
                //{
                //    var img = UploadImage(oss, logoUrl);
                //    if (img != null) dto.LogoUrl = img.OssUrl;
                //}
                //if (imageUrl != null)
                //{
                //    var img = UploadImage(oss, imageUrl);
                //    if (img != null) dto.ImageUrl = img.OssUrl;
                //}

                var id = TenantBO.Instance.Add(dto);
                LogAction(ActionType.新增, $"新增租户：{id}-{dto.TenantName}");

                FineUICore.PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                //ActiveWindow.HidePostBack();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }

            return UIHelper.Result();
        }

    }
}